#include "StdAfx.h"
#include "DTPCtrl.h"

CDTPCtrl::CDTPCtrl(void)
{
}

CDTPCtrl::~CDTPCtrl(void)
{
}

int CDTPCtrl::Update2NowTime(HWND hDlg, int nIDDate, int nIDTime)
{
    SYSTEMTIME sysTime;
    GetLocalTime(&sysTime);

	HWND hwnd = GetDlgItem(hDlg, nIDDate);
    CDateTimeCtrl* pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
    VERIFY(pDT->SetTime(&sysTime));

	hwnd = GetDlgItem(hDlg, nIDTime);
    pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
    VERIFY(pDT->SetTime(&sysTime));

    return 0;
}

CTime CDTPCtrl::GetDateTimePicker(HWND hDlg, int nIDDate, int nIDTime)
{
    SYSTEMTIME temp;
    CTime nowDate;
    CTime nowTime;

    HWND hwnd = GetDlgItem(hDlg, nIDDate);
	CDateTimeCtrl* pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
	DWORD dwResult1 = pDT->GetTime(nowDate);

	hwnd = GetDlgItem(hDlg, nIDTime);
	pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
    DWORD dwResult2 = pDT->GetTime(nowTime);

    if (dwResult1 == GDT_VALID && dwResult2 == GDT_VALID)
    {
        temp.wYear = nowDate.GetYear();
        temp.wMonth = nowDate.GetMonth();
        temp.wDay = nowDate.GetDay();
        temp.wHour = nowTime.GetHour();
        temp.wMinute = nowTime.GetMinute();
        temp.wSecond = nowTime.GetSecond();

        nowTime = CTime(temp.wYear, temp.wMonth, temp.wDay, temp.wHour, temp.wMinute, temp.wSecond, -1);

        return nowTime;
    } 
    else
    {
        AfxMessageBox(_T("Time Set Error!"));
        return 0;
    }
}

BOOL CDTPCtrl::SetDateTimePickerFormat(HWND hDlg, int nIDDate, int nIDTime)
{
    HWND hwnd = GetDlgItem(hDlg, nIDDate);
    CDateTimeCtrl* pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
    if (!pDT->SetFormat(_T("yyyy-MM-dd ddd")))
    {
        return FALSE;
    }

    hwnd = GetDlgItem(hDlg, nIDTime);
    pDT = (CDateTimeCtrl*)CWnd::FromHandle(hwnd);
    if (!pDT->SetFormat(_T("HH:mm:ss")))
    {
        return FALSE;
    }

    return TRUE;
}